package router

import (
	"ETikTok/controller"
	"ETikTok/logger"
	"ETikTok/middlewares"
	"net/http"

	"github.com/gin-gonic/gin"
)

func Setup(mode string) *gin.Engine {
	if mode == "release" {
		gin.SetMode(gin.ReleaseMode)
	}
	r := gin.New()
	//r.Use(logger.GinLogger(), logger.GinRecovery(true), middlewares.RateLimitMiddleware(2*time.Second, 1))
	r.Use(logger.GinLogger(), logger.GinRecovery(true))
	v1 := r.Group("/api/v1")

	// 注册
	v1.POST("/signup", controller.SignUpHandler)
	// 登录
	v1.POST("/login", controller.LoginHandler)

	v1.GET("/user", controller.GetUserInfoHandler)
	v1.GET("/tag", controller.TagHandler)
	v1.GET("/tag/:id", controller.TagDetailHandler)
	v1.GET("/video/:id", controller.GetVideoDetailHandler)
	v1.GET("/videos", controller.GetVideoListHandler)
	v1.GET("/videos2", controller.GetVideoListHandler2)

	v1.Use(middlewares.JWTAuthMiddleware()) // 使用JWT认证中间件

	r.LoadHTMLFiles("./templates/index.html")
	r.Static("./static", "static")
	r.GET("/", func(ctx *gin.Context) {
		ctx.HTML(http.StatusOK, "index.html", nil)
	})

	{
		v1.POST("/video", controller.CreateVideoHandler)
		v1.POST("/vote", controller.VideoVoteHandler)
	}

	r.NoRoute(func(ctx *gin.Context) {
		ctx.JSON(http.StatusNotFound, gin.H{
			"msg": "404",
		})
	})

	r.StaticFS("/videos", http.Dir("./videos"))

	return r
}
